﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EncryptProject
{
    /// <summary>
    /// 用于加解密
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // eg: Database=pl;Data Source=localhost;port=3306;User Id=root;Password=root
                string connectionStringFormat = "Database={0};Data Source={1};port={2};User Id={3};Password={4}";
                string database, dataSource, portNo, userID, password, key;

                Console.WriteLine("------------------------------");
                Console.WriteLine("********** DES 加密 **********");
                Console.WriteLine("------------------------------");

                Console.Write(Environment.NewLine);
                Console.Write("Database（数据库名称）：");
                database = Console.ReadLine();
                Console.Write("Data Source（数据库地址，若为本地，请填“localhost”）：");
                dataSource = Console.ReadLine();
                Console.Write("Port Number（端口号，默认请填“3306”）：");
                portNo = Console.ReadLine();
                Console.Write("User Id（登录名）：");
                userID = Console.ReadLine();
                Console.Write("Password（密码）：");
                password = Console.ReadLine();
                Console.Write("Key（无密钥请直接按回车键）：");
                key = Console.ReadLine();

                string connectionString = string.Format(connectionStringFormat, database,
                    dataSource, portNo, userID, password);

                Console.Write(Environment.NewLine);
                Console.WriteLine("DES加密：");
                string encryptString = EncryptAlgorithmHelper.Encrypt2(connectionString, key);
                Console.WriteLine(encryptString);

                Console.Write(Environment.NewLine);
                Console.WriteLine("DES解密：");
                string decryptString = EncryptAlgorithmHelper.Decrypt2(encryptString, key);
                Console.WriteLine(decryptString);
                Console.WriteLine(Environment.NewLine);

                Console.WriteLine("------------------------------");
                Console.WriteLine("++++++++++ AES 加密 ++++++++++");
                Console.WriteLine("------------------------------");

                string inputString; // 密码明文
                string extString; // 密码附加信息


                Console.Write(Environment.NewLine);
                Console.Write("Password（密码）：");
                inputString = Console.ReadLine();
                Console.Write("Ext（附加信息）：");
                extString = Console.ReadLine();

                Console.Write(Environment.NewLine);
                Console.WriteLine("AES加密：");
                string aesString = EncryptAlgorithmHelper.Encrypt(inputString, extString);
                Console.WriteLine(aesString);
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                Console.ReadKey(true);
            }
        }
    }
}
